package com.ss.android.update;

import android.content.Context;
import android.util.Log;

import com.ss.android.common.util.NetworkUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public final class b {
    private Context a;
    private UpdateHelper b;
    private volatile boolean c;

    public b(UpdateHelper arg2, Context arg3) {

        this.b = arg2;
        this.a = arg3;
        this.c = false;
    }

    final boolean a(String arg20, String arg21, boolean arg22, Exception[] arg23) {
        a v10_1;
        UpdateHelper v6_3;
        Exception v18_2;
        InputStream v9_2 = null;
        String v8_1;
        String v9_1;
        int v3_3;
        int v11 = 0;
        FileOutputStream v4_1;
        URLConnection v2_3 = null;
        int v9;
        int v6_1;
        Exception v3_1;
        FileOutputStream v5 = null;
        File v13 = null;
        int v8;
        byte[] v12;
        Exception v6 = null;
        IOException v3 = null;
        int v2 = 4096;
        try {
            v12 = new byte[v2];
            v8 = 0;
            v13 = new File(arg21);
            v5 = new FileOutputStream(v13, true);
            v6_1 = 0;
            v9 = 0;
            while (true) {
                if (v9 >= 4) {
                    return false;
                }
                try {
                    v2_3 = new URL(arg20).openConnection();
                    ((HttpURLConnection) v2_3).setInstanceFollowRedirects(true);
                    ((HttpURLConnection) v2_3).setConnectTimeout(6000);
                    ((HttpURLConnection) v2_3).setReadTimeout(6000);
                    v11 = v9 + 1;
                    if (v6_1 > 0) {
                        ((HttpURLConnection) v2_3).addRequestProperty("Range", "bytes=" + v6_1 + "-");
                    }
                    v3_3 = ((HttpURLConnection) v2_3).getResponseCode();
                    if ((v6_1 != 0 || v3_3 == 200) && (v6_1 <= 0 || v3_3 == 206)) {

                    } else {
                        Log.w("UpdateDownloader", "download failed with code " + v3_3);
                        return false;
                    }
                } catch (IOException ex) {
                    if (!NetworkUtils.isNetworkAvailable(this.a)) {
                        break;
                    }
                    ((HttpURLConnection) v2_3).disconnect();
                    v9 = v11;
                    continue;
                }

                if (v6_1 == 0) {
                    v9_1 = null;
                    String v10 = null;
                    String v3_5 = v2_3.getHeaderField("Transfer-Encoding");
                    if (v3_5 != null) {
                        v10 = v3_5;
                    }
                    if (v10 == null) {
                        v3_5 = v2_3.getHeaderField("Content-Length");
                        if (v3_5 != null) {
                            String v18_1 = v3_5;
                            v3_3 = Integer.parseInt(v3_5);
                            v8_1 = v18_1;
                        } else {
                            v3_3 = v8;
                            v8_1 = v9_1;
                        }
                    } else {
                        v3_3 = -1;
                        v8_1 = v9_1;
                    }
                    if (v8_1 == null && (v10 == null || !v10.equalsIgnoreCase("chunked"))) {
                        Log.w("UpdateDownloader", "can not know content length, give up");
                        return false;
                    } else {
                        this.b.a(v3_3, "", arg22);
                        v8 = v3_3;
                    }
                }
                try {
                    v9_2 = ((HttpURLConnection) v2_3).getInputStream();
                } catch (IOException ex) {
                    if (!NetworkUtils.isNetworkAvailable(this.a)) {
                        break;
                    }
                    Log.d("UpdateDownloader", "IOException when getting enttity for " + arg20 + ": " + ex);
                    ((HttpURLConnection)v2_3).disconnect();
                    v9 = v11;
                    continue;
                }
                v3_3 = v6_1;
                while (true) {
                    try {
                        v6_1 = v9_2.read(v12);
                    } catch (IOException ex) {
                        Log.d("UpdateDownloader", "read failed");
                        v9_2.close();
                        ((HttpURLConnection) v2_3).disconnect();
                        v9 = v11;
                        v6_1 = v3_3;
                        break;
                    }

                    if (v6_1 != -1) {
                        v3_3 += v6_1;
                        try {
                            v5.write(v12, 0, v6_1);
                            if ((((long) v3_3)) <= 52428800) {
                                v6_3 = this.b;
                                v10_1 = v6_3.n;
                                synchronized (v10_1) {
                                    v6_3.n.a = v3_3;
                                    v6_3.n.b = v8;
                                }
                                continue;
                            } else {
                                Log.w("UpdateDownloader", "exceed max_apk_size: " + v3_3);
                                return false;
                            }
                        } catch (Exception v4_2) {
                            Log.w("UpdateDownloader", "can not write data: " + v4_2);
                        }
                    }
                    if (v8 <= 0 || v8 == v3_3) {
                        return true;
                    } else {
                        Log.w("UpdateDownloader", "content length mismatch");
                        return false;
                    }
                }
            }
        }catch (Exception e){
            Log.w("UpdateDownloader", "download exception: " + e);
            v13.delete();
            return false;
        }finally {
           if(v5 != null){
               try {
                   v5.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }

            if(v9_2 != null){
                try {
                    v9_2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(v2_3 != null){
                try {
                    ((HttpURLConnection) v2_3).disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
}

